package com.liuzhilin.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DatabaseOperations {
    // 增删改查
    public static void executeUpdate(String sql,Object[] objects){
        try {
            Connection connection = JDBC.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < objects.length; i++) {
                preparedStatement.setObject(i+1,objects[i]);
            }
            preparedStatement.executeUpdate();
            JDBC.returnConnection(connection);
        }catch (Exception E){
            System.out.println(E);
        }
    }
    // 查询
    public static ResultSet executeQuery(String sql, Object[] objects){
        try {
            Connection connection = JDBC.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            if (objects==null){
                ResultSet resultSet = preparedStatement.executeQuery();
                JDBC.returnConnection(connection);
                return resultSet;
            }
            for (int i = 0; i < objects.length; i++) {
                preparedStatement.setObject(i+1,objects[i]);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            JDBC.returnConnection(connection);
            return resultSet;
        }catch (Exception E){
            System.out.println(E);
            return null;
        }

    }


}
